package com.adtech.adms.commons.web.tag;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.commons.util.TreeUtil;
import com.adtech.adms.commons.util.TreeUtil.OnGetNodeListener;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.User;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Dormin 
 * @since 2011-5-31 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class PermissionTag extends AbstractUITag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4238421068364640444L;

	/**
	 * 模块代码
	 */
	private String featureCode;

	private String image;
	private String href;
	private String icon;

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getFeatureCode() {
		return featureCode;
	}

	public void setFeatureCode(String featureCode) {
		this.featureCode = featureCode;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;

	}

	private Feature feature;

	@Override
	public Component getBean(ValueStack valueStack, HttpServletRequest request, HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute(SessionKey.SESSION_KEY_USER);
		List<TreeNode<Feature>> nodes = user.getFeaturesTree();
		TreeNode<Feature> node = null;
		for (TreeNode<Feature> treeNode : nodes) {
			node = TreeUtil.iteratorNodes(treeNode, new OnGetNodeListener<Feature>() {
				public boolean onGetNode(TreeNode<Feature> node) {
					if (featureCode.equals(node.getInnerObject().getCode())) {
						return true;
					}
					return false;
				}

			});
			if (node != null) {
				feature = node.getInnerObject();
				break;
			}
		}

		return new PermissionComponent(valueStack, request, response);
	}

	@Override
	public int doStartTag() throws JspException {

		int status = super.doStartTag();
		if (feature == null) {
			status = SKIP_BODY;
		}
		return status;
	}

	@Override
	protected void populateParams() {

		super.populateParams();
		PermissionComponent buComponent = (PermissionComponent) component;
		String path = ServletActionContext.getRequest().getContextPath();
		String basePath = ServletActionContext.getRequest().getScheme() + "://"
		+ ServletActionContext.getRequest().getServerName() + ":"
		+ ServletActionContext.getRequest().getServerPort() + path + "/";

		buComponent.setFeature(feature);
		buComponent.setImage(basePath + image);
		buComponent.setIcon(icon);
		if (href != null) {
			buComponent.setHref(basePath + href);
		}
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

}
